package org.destiny.account.richclient.tabpage;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;

import org.destiny.account.entity.Customer;
import org.destiny.account.service.CustomerService;

public class AllCustomerPage extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public AllCustomerPage() {
		super(new BorderLayout(10, 10));

		JPanel topPane = new JPanel(new FlowLayout(FlowLayout.CENTER));
		topPane.add(new JTextField(30));
		topPane.add(new JButton("search"));

		JTable table = new JTable(
				new CustomersTableModel((ArrayList<Customer>) CustomerService
						.get().selectAll()));
		table.setComponentPopupMenu(createPopupMenu());
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

		add(topPane, BorderLayout.NORTH);
		add(scrollPane, BorderLayout.CENTER);

		JPanel bottomP = new JPanel(new BorderLayout());

		JPanel btnsPane = new JPanel();
		JButton b1 = new JButton("全");
		JButton b2 = new JButton("反");
		JButton b3 = new JButton("取");
		btnsPane.add(b1);
		btnsPane.add(b2);
		btnsPane.add(b3);

		JPanel btnsPane2 = new JPanel(new FlowLayout(FlowLayout.RIGHT, 20, 10));
		JLabel l = new JLabel("共12页");
		JButton b21 = new JButton("下一页");
		JTextField ff = new JTextField(3);
		JLabel ll = new JLabel("<html><u>跳转</u>");
		JButton b22 = new JButton("上一页");
		btnsPane2.add(l);
		btnsPane2.add(b21);
		btnsPane2.add(ff);
		btnsPane2.add(ll);
		btnsPane2.add(b22);

		bottomP.add(btnsPane, BorderLayout.NORTH);
		bottomP.add(btnsPane2, BorderLayout.CENTER);
		add(bottomP, BorderLayout.SOUTH);
	}
	
	private JPopupMenu createPopupMenu(){
		JPopupMenu pop = new JPopupMenu();
		pop.add("删除此客户");
		pop.add("删除已选客户");
		pop.addSeparator();
		pop.add("新添客户");
		return pop;
	}

	public class CustomersTableModel extends AbstractTableModel {
		// name;
		// gender;
		// township;
		// village;
		// remark;
		private static final long serialVersionUID = 1L;
		private boolean[] checked;
		private ArrayList<Customer> vector;

		public CustomersTableModel(ArrayList<Customer> v) {
			checked = new boolean[v.size()];
			this.vector = v;
		}

		@Override
		public Class<?> getColumnClass(int col) {
			if (col == 0)
				return Boolean.class;
			else
				return String.class;
		}

		@Override
		public String getColumnName(int col) {
			switch (col) {
			case 0:
				return "选择";
			case 1:
				return "姓名";
			case 2:
				return "性别";
			case 3:
				return "乡镇";
			case 4:
				return "村";
			case 5:
				return "备注";
			default:
				return null;
			}
		}

		@Override
		public int getRowCount() {
			return vector.size();
		}

		@Override
		public int getColumnCount() {
			return 6;
		}

		@Override
		public Object getValueAt(int row, int col) {
			switch (col) {
			case 0:
				return checked[row];
			case 1:
				return vector.get(row).getName();
			case 2:
				return vector.get(row).getGender();
			case 3:
				return vector.get(row).getTownship();
			case 4:
				return vector.get(row).getVillage();
			case 5:
				return vector.get(row).getRemark();
			default:
				return null;
			}
		}

		@Override
		public void setValueAt(Object aValue, int row, int col) {
			if (col == 0)
				checked[row] = (Boolean) aValue;
		}

		@Override
		public boolean isCellEditable(int row, int col) {
			return col == 0;
		}
	}

}
